var cf = require("../../config.js");
var util = require("../../utils/util.js");
var cusmallToken = wx.getStorageSync('cusmallToken');
var baseHandle = require("../template/baseHandle.js");
//获取应用实例
var app = getApp();
Page(Object.assign({}, baseHandle,{

  /**
   * 页面的初始数据
   */
  data: {
    extConfig: wx.getExtConfigSync ? wx.getExtConfigSync() : {},
    app: app,
    staticResPath: cf.config.staticResPath,
    userImagePath: cf.config.userImagePath,
    videoMainUrl:"",
    videoUrl:"",
    isVVideo:""
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('video',options);
    var ctx = this;
    ctx.setData({
      eId: options.eId || wx.getStorageSync("eId") || "",
      videoUrl: options.vUrl+'?'+new Date().getTime()
    });
    if("y" == options.isVVideo){//是否去分析 腾讯视频链接
      ctx.video_analysis(options.vUrl);
      ctx.setData({
        isVVideo: options.isVVideo,
      });
    }else{
      ctx.setData({
        videoMainUrl: options.vUrl+'?'+new Date().getTime(),
      });
    } 
  },
  // 处理腾讯视频
  video_analysis(origUrl){
    var ctx = this;
    wx.request({
      url: cf.config.pageDomain + "/applet/mobile/video_analysis/analysis",
      data: {
        cusmallToken: wx.getStorageSync('cusmallToken'),
        origUrl: origUrl
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;
        if (data && 0 === data.ret) {
          ctx.setData({
            videoMainUrl: data.model.url
          });
        }
      },
      fail() {
       
      },
      complete() {
        ctx.setData({ isLoading: false });
        // wx.hideLoading();
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.videoContext = wx.createVideoContext('video', this);

    this.videoContext.requestFullScreen({ direction: 0 });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let ctx = this;
    let path = "/pages/single_video/video_play?vUrl=" + ctx.data.videoUrl + "&isVVideo=" + ctx.data.isVVideo + "&eId=" + ctx.data.eId + "&source=1";
    let cusmallToken = wx.getStorageSync('cusmallToken');
    util.addRelation(ctx.data.eId, cusmallToken, 1);
    let shareObj = {
      title: "很有精彩的视频",
      path: path,
      success: function (res) {
        // 成功
      },
      fail: function (res) {
        // 失败
      }
    };
    console.log(shareObj)

    return shareObj;

  }
}))